package org.hentai.bt.util.sql;

import org.hentai.bt.anno.field.base.FieldBase;

import java.lang.annotation.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 通过注解获取数据类型
 */
public class SqlDataTypeUtil {
    public static SqlDataType getDatatype(Annotation[] annotations, SqlDataType dataType) {

        List<Annotation> annotationList = new ArrayList<>(Arrays.asList(annotations));

        annotationList.removeIf(
                next -> next.annotationType() == Target.class
                        || next.annotationType() == Documented.class
                        || next.annotationType() == Retention.class
                        || next.annotationType() == Inherited.class
        );

        for (Annotation annotation : annotationList) {
            if (annotation.annotationType().equals(FieldBase.class)) {
                FieldBase fieldBase = (FieldBase) annotation;
                SqlDataType type = fieldBase.dataType();
                dataType = type != null ? type : dataType;
                return dataType;
            }

            if (annotation.annotationType().isAnnotationPresent(FieldBase.class)) {
                FieldBase fieldBase = annotation.annotationType().getAnnotation(FieldBase.class);
                SqlDataType type = fieldBase.dataType();
                dataType = type != null ? type : dataType;
                return dataType;
            }

            return getDatatype(annotation.annotationType().getAnnotations(), dataType);
        }

        return dataType;
    }
}
